export function setupSocket(server) {
  const io = require("socket.io")(server, {
    path: "/socket",
    cors: {
      // https://socket.io/zh-CN/docs/v4/handling-cors
      origin: true,
    },
  });

  io.on("connection", (client) => {
    const { oid } = client.handshake.query;
    if (oid) {
      client.join("order-" + oid);
    }
    // client.on("disconnect", () => {});
    client.on("login", (data) => {
      console.log(data);
      // client.emit("hello", {});
    });
  });

  global.sendRoomMsg = function (room, ev, data) {
    // console.log(room, ev, data);
    io.to(room).emit(ev, data);
  };
}
